package ssm.dao;

import org.apache.ibatis.annotations.*;
import ssm.domain.Role;
import ssm.domain.UserInfo;

import java.awt.*;
import java.util.List;

public interface IUserDao {

    @Select("select * from users where username = #{username}")

    /*同时获取角色信息.此处主要针对User与角色Role之间多对多关系的查询。将查询进行封装*/
    /*其中的id是用来说明，是否为主键。*/
    @Results({
            @Result(id = true ,property ="id" ,column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "email",column = "email"),
            @Result(property = "password",column = "password"),
            @Result(property = "phoneNum",column = "phoneNum"),
            @Result(property = "status",column = "status"),

            /*此处为多对多的查询，与结果封装*/
            @Result(property = "roles",column = "id",javaType = java.util.List.class,
                    /*通过指定查询语句的路径，达到查询的效果*/
                    many = @Many(select = "ssm.dao.IRoleDao.findRoleByUserId"))
    })
    public UserInfo findByUsername (String username) throws  Exception;

    @Select("select * from users")
    List<UserInfo> findAll() throws Exception;

    @Insert("insert into users(id,email,username,password,phoneNum,status) value(#{phoneNum},#{email},#{username},#{password},#{phoneNum},#{status})")
    void save(UserInfo userInfo) throws Exception;

    /*此处不仅要查询id对应的user基本信息，还有和该id挂钩的role,*/
    @Select("select * from users where id = #{id}")
    @Results({
            @Result(id = true ,property ="id" ,column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "email",column = "email"),
            @Result(property = "password",column = "password"),
            @Result(property = "phoneNum",column = "phoneNum"),
            @Result(property = "status",column = "status"),

            @Result(property = "roles",column = "id",javaType = java.util.List.class,
                    /*通过指定查询语句的路径，达到查询的效果*/
                    many = @Many(select = "ssm.dao.IRoleDao.findRoleByUserId"))
    })
    UserInfo findById(String id) throws Exception;

    @Select("select * from role where id not in (select roleId from users_role where userId = #{useId})")
    List<Role> findOtherRoles(String userId);

    @Insert("insert into users_role(userId,roleId) values(#{userId},#{roleId}) ")
    /*此处的添加是将每一个roleID对应的内容添加到user下。因此需要对每一个参数使用@Param注解*/
    void addRoleToUser(@Param("userId") String userId, @Param("roleId") String roleId);
}
